package com.faradel.pharmapp.managedbean;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.faradel.pharmapp.entities.Patient;
import org.faradel.pharmapp.entities.PatientOrder;
import org.faradel.pharmapp.entities.PrescriptionOrder;
import org.springframework.context.ApplicationContext;
import org.springframework.web.jsf.FacesContextUtils;

import com.faradel.pharmapp.service.PatientOrderServiceInterface;
import com.faradel.pharmapp.service.PatientServiceInterface;

@ManagedBean
@ViewScoped
public class FindOrderBean implements Serializable
{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Resource
    PatientOrder patientOrder;

    @Resource
    Patient patient;

    @Resource
    PrescriptionOrder prescriptionOrder;

    // patientOrder id
    private String patientOrderId;
    private List<PatientOrder> patientOrderList;
    private String status;
    private String action;

    Map<String, String> searchMap;

    {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        // Patient instance
        ApplicationContext context = FacesContextUtils.getWebApplicationContext(facesContext);
        patient = (Patient) context.getBean("patient");

        action = facesContext.getExternalContext().getRequestParameterMap().get("action");
        // get patientOrder instance
        patientOrder = (PatientOrder) context.getBean("patientOrder");

        // get PrescriptionOrder instance
        prescriptionOrder = (PrescriptionOrder) context.getBean("prescriptionOrder");

        // Patient Order List
        patientOrderList = new ArrayList<PatientOrder>();
        searchMap = new HashMap<String, String>();
    }

    public PatientOrder getPatientOrder()
    {
        return patientOrder;
    }

    public void setPatientOrder(PatientOrder patientOrder)
    {
        this.patientOrder = patientOrder;
    }

    public Patient getPatient()
    {
        return patient;
    }

    public void setPatient(Patient patient)
    {
        this.patient = patient;
    }

    public PrescriptionOrder getPrescriptionOrder()
    {
        return prescriptionOrder;
    }

    public void setPrescriptionOrder(PrescriptionOrder prescriptionOrder)
    {
        this.prescriptionOrder = prescriptionOrder;
    }

    private PatientServiceInterface getPatientServiceInterface()
    {
        ApplicationContext context = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
        return (PatientServiceInterface) context.getBean("patientServiceImpl");
    }

    private PatientOrderServiceInterface getPatientOrderServiceInterface()
    {
        ApplicationContext context = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
        return (PatientOrderServiceInterface) context.getBean("patientOrderServiceImpl");
    }

    /**
     * Find patient using firstname last name and date of birth
     */
    public void searchByPatient()
    {
        clearContainers();
        searchMap.put("firstName", patient.getFirstName());
        searchMap.put("lastName", patient.getLastName());

        DateFormat df = new SimpleDateFormat("y-MM-d");
        searchMap.put("dob", df.format(patient.getDob()));

        List<Patient> patientList = getPatientServiceInterface().getPatientByColumns(searchMap);

        Map<String, String> map = new HashMap<String, String>();
        for (Patient p : patientList)
        {
            map.put("patient_id", String.valueOf(p.getPatientId()));
            patientOrderList = getPatientOrderServiceInterface().getPatientOrderByColumns(map);
        }
    }

    /**
     * Search by Id
     */
    public void searchById()
    {
        clearContainers();

        int id = Integer.parseInt(patientOrderId);
        patientOrder = getPatientOrderServiceInterface().getPatientOrderById(id);
        patientOrderList.add(patientOrder);
    }

    /**
     * Search by order status
     */
    public void searchByStatus()
    {
        clearContainers();

        searchMap.put("status", status);
        patientOrderList = getPatientOrderServiceInterface().getPatientOrderByColumns(searchMap);
    }

    /**
     * Make sure map and list have no values
     */
    private void clearContainers()
    {
        if (searchMap.size() > 0)
            searchMap.clear();
        if (patientOrderList.size() > 0)
            patientOrderList.clear();
    }

    public String updateOrder()
    {
        setAction("update");
        return "processprescription";
    }

    public String deleteOrder()
    {
        setAction("delete");
        return "processprescription";
    }

    public String viewOrder()
    {
        setAction("find");
        return "processprescription";
    }

    public List<PatientOrder> getPatientOrderList()
    {
        return patientOrderList;
    }

    public void setPatientOrderList(List<PatientOrder> patientOrderList)
    {
        this.patientOrderList = patientOrderList;
    }

    public Map<String, String> getSearchMap()
    {
        return searchMap;
    }

    public void setSearchMap(Map<String, String> searchMap)
    {
        this.searchMap = searchMap;
    }

    public String getStatus()
    {
        return status;
    }

    public void setStatus(String status)
    {
        this.status = status;
    }

    public String getPatientOrderId()
    {
        return patientOrderId;
    }

    public void setPatientOrderId(String patientOrderId)
    {
        this.patientOrderId = patientOrderId;
    }

    public String getAction()
    {
        return action;
    }

    public void setAction(String action)
    {
        this.action = action;
    }
}
